﻿using C3.XNA;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RpgXna.Model.Helpers;
using RpgXna.Model.SpriteBase;
using RpgXna.Model.XnaClassesWrappers;

namespace RpgXna.Model.Entities.MainCharacter
{

    public partial class Character : CollidableSprite
    {
        public Character(InputManager input)
        {
            SetAttributes();
            Origin = Vector2.Zero;
            Rotation = 0f;
            Position = new Vector2(600, 500);
            Speed = new Vector2(normalVelocity);

        }



        public void CancelMove(GameTime theGameTime, IKeyboardStateWrapper aCurrentKeyboardState)
        {
            InternalMove(theGameTime, aCurrentKeyboardState, false);
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(Texture, Position, null, Color.White, Rotation, Origin, 1f, SpriteEffects.None, 0); //main sprite
            sb.DrawRectangle(Box, Color.Red, 2);

            DrawStaminaBar(sb);
        }

        public void Load(ContentManager cm)
        {
            LoadTexture(cm.Load<Texture2D>("man2_scale_40x40"));
        }


        public void Update(GameTime gt, IKeyboardStateWrapper kbs)
        {
            InternalMove(gt, kbs);
        }

        private void DrawStaminaBar(SpriteBatch sb)
        {
            var staminaBounds = new Rectangle(20, 20, _staminaGauge / 100, 20);
            sb.FillRectangle(new Rectangle(21, 21, Stamina * 10, 19), Color.White);
            sb.FillRectangle(staminaBounds, Color.Green);
            sb.DrawRectangle(new Rectangle(staminaBounds.X, staminaBounds.Y, Stamina * 10, staminaBounds.Height),
                             Color.Black, 3);
        }

        private int HandleRunning(GameTime gt, IKeyboardStateWrapper kbstate)
        {
            int speed = normalVelocity;
            bool tryRun = kbstate.IsKeyDown(InputBindings.Bindings.Run) && kbstate.IsAnyKeyDown(InputBindings.Bindings.MoveBindings);

            //if character is running
            if (tryRun)
            {
                //si la jauge de stamina est suffisament remplie
                if (_staminaGauge > 0)
                {
                    //on décroit la jauge d'endurance
                    _staminaGauge -= gt.ElapsedGameTime.Milliseconds * 2;

                    //et on retourne la vitesse normale majorée par la vitesse de course
                    speed = normalVelocity + runVelocitybonus;
                }
            }

            if (speed == normalVelocity && !tryRun)
            {
                //deplacement normal
                //ici le repos peut durer très très longtemps (tant que le joueur ne courre pas) donc ont stope le compteur de repos dès qu'il est suffisant que courrir  de nouveau
                if (_staminaGauge < MaxStamina)
                    _staminaGauge += gt.ElapsedGameTime.Milliseconds;
            }
            return speed;
        }

        private void InternalMove(GameTime gt, IKeyboardStateWrapper kbstate, bool forward = true)
        {
            int speed = HandleRunning(gt, kbstate);

            //stuff to get the move wrapper data valorized (new position, speed, rotation, etc.)
            MoveWrapper pm = Movements.Move(Position, Rotation, kbstate, speed, gt, forward);
            Position = pm.Position;
            Rotation = pm.Rotation;
            Speed = pm.Speed;
            Direction = pm.Direction;
        }
    }
}